﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ARPGDATA.Maps;

namespace ARPGDATA
{
    public class MapLoader
    {
        /// <summary>
        /// 获取地图信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static ARPGDATA.Maps.AGRPGMap LoadMap(int id)
        {
            int step = 0;
            string mapFilePath = string.Format("{0}datas\\maps\\{1:d4}.map", AppDomain.CurrentDomain.BaseDirectory, id);
            if (!File.Exists(mapFilePath))
            {
                return null;
            }

            AGRPGMap map = new AGRPGMap();

            using (Stream stream = new FileStream(mapFilePath, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string text = reader.ReadLine();
                    while (!string.IsNullOrEmpty(text))
                    {
                        if (!text.StartsWith("-"))
                        {
                            if (step == 0)
                            {
                                // id
                                map.Id = Convert.ToInt32(text);
                            }
                            else if (step == 1)
                            {
                                map.Version = Convert.ToInt32(text);
                            }
                            else if (step == 2)
                            {
                                map.Row = Convert.ToInt32(text);
                            }
                            else if (step == 3)
                            {
                                map.Col = Convert.ToInt32(text);
                                map.Cells = new AGRPGMapCell[map.Row * map.Col];
                            }
                            else if (step > 3 && step < 4 + map.Row)
                            {
                                // foreground
                                int row = step - 4;
                                string[] arr = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                for (int col = 0; col < arr.Length; col++)
                                {
                                    AGRPGMapCell cell = new AGRPGMapCell();
                                    cell.ForegroundId = Convert.ToByte(arr[col], 16);
                                    map.Cells[row * map.Col + col] = cell;
                                }
                            }
                            else if (step > 3 + map.Row && step < 4 + 2 * map.Row)
                            {
                                // background
                                int row = step - (4 + map.Row);
                                string[] arr = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                for (int col = 0; col < arr.Length; col++)
                                {
                                    AGRPGMapCell cell = map.Cells[row * map.Col + col];
                                    cell.BackgroundId = Convert.ToByte(arr[col], 16);
                                }
                            }
                            else if (step > 3 + 2 * map.Row && step < 4 + 3 * map.Row)
                            {
                                // tile type
                                int row = step - (4 + 2*map.Row);
                                string[] arr = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                for (int col = 0; col < arr.Length; col++)
                                {
                                    AGRPGMapCell cell = map.Cells[row * map.Col + col];
                                    cell.TileTypeId = Convert.ToByte(arr[col], 16);
                                }
                            }
                            else if (step > 3 + 3 * map.Row && step < 4 + 4 * map.Row)
                            {
                                // tile direction
                                int row = step - (4 + 3 * map.Row);
                                string[] arr = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                for (int col = 0; col < arr.Length; col++)
                                {
                                    AGRPGMapCell cell = map.Cells[row * map.Col + col];
                                    cell.TileDirectionId = Convert.ToByte(arr[col], 16);
                                }
                            }
                            else if (step > 3 + 4 * map.Row && step < 4 + 5 * map.Row)
                            {
                                // tile index
                                int row = step - (4 + 4 * map.Row);
                                string[] arr = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                for (int col = 0; col < arr.Length; col++)
                                {
                                    AGRPGMapCell cell = map.Cells[row * map.Col + col];
                                    cell.TileIndexId = Convert.ToByte(arr[col], 16);
                                }
                            }
                            step++;
                        }
                        text = reader.ReadLine();
                    }
                }
            }
            return map;
        }
    }
}
